package org.liuwei.concucrent.base02_cas.AtomicIntegerFieldUpdater_07;

import java.util.concurrent.atomic.*;

class Container {
	public volatile int no;
}

/**
 * 
 * @Title: UpdaterUsage.java
 * @Package org.liuwei.concucrent.base02_cas.AtomicIntegerFieldUpdater_07
 * @Description: TODO(AtomicIntegerfieldupdater 实现了普通变量的原子操作 ，加法减都可以)
 * @author 刘伟 15818570028@163.com
 * @date 2016年8月20日 下午6:34:50
 * @version V1.0
 */
class Task extends Thread {
	private AtomicIntegerFieldUpdater<Container> updater = AtomicIntegerFieldUpdater.newUpdater(Container.class, "no");
	private Container c;

	public Task(Container c) {
		this.c = c;
	}

	@Override
	public void run() {
		for (int i = 0; i < 1000; i++) {
			System.out.println(updater.getAndIncrement(c));
			System.out.println(updater.getAndIncrement(c));
		}

	}
}

public class UpdaterUsage {
	public static void main(String[] args) throws InterruptedException {
		Container c = new Container();
		Task t1 = new Task(c);
		Task t2 = new Task(c);
		t1.start();
		t2.start();
		Thread.sleep(100);
		System.err.println("我的值现在是：" + c.no);
	}
}